//
//  AlterViewController.m
//  ECO
//
//  Created by zl on 2019/9/27.
//  Copyright © 2019 谷银行. All rights reserved.
//

#import "AlterViewController.h"

@interface AlterViewController ()<UITextFieldDelegate>
@property(nonatomic , strong)UITextField *Textfiled;
@end

@implementation AlterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(WanchengBtn)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    
    self.Textfiled = [[UITextField alloc]init];
    self.Textfiled.backgroundColor = [UIColor whiteColor];
    self.Textfiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.Textfiled];
    [_Textfiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(15);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.equalTo(@(44));
    }];
    if (self.Title == nil) {
        self.navigationItem.title = @"添加分组";

    }else{
        self.navigationItem.title = @"修改分组";
        self.Textfiled.text = [NSString stringWithFormat:@"%@",self.Title];
    }
}

// 完成
- (void)WanchengBtn{
    NSUserDefaults *User = [NSUserDefaults standardUserDefaults];
    NSLog(@"%@",[User objectForKey:@"kfId"]);
    if (self.Title == nil) {
        if ([self.Textfiled.text isEqualToString:@""]) {
            [BRProgressHUD showErrorWithStatus:@"请输入分组名"];
            [BRProgressHUD dismissWithDelay:1.25];
            return;
        }else{
            // 添加分组
            NSDictionary *dic = @{
                                @"groupName":self.Textfiled.text,
                                @"kfUserId":[User objectForKey:@"kfId"],
                                @"groupType":@"1",
                                @"pubPriType":@"1"
                                };
            NSLog(@"+++%@",dic);
            [HttpsTool post:@"/web/group/recovery/pri/add" params:dic success:^(id response) {
                NSLog(@"%@",response);
                NSLog(@"%@",response[@"msg"]);
                [BRProgressHUD showErrorWithStatus:response[@"msg"]];
                [BRProgressHUD dismissWithDelay:1.25];
                [self.navigationController popViewControllerAnimated:YES];
            } failure:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
    }else{
        if ([self.Textfiled.text isEqualToString:@""]) {
            [BRProgressHUD showErrorWithStatus:@"请输入分组名"];
            [BRProgressHUD dismissWithDelay:1.25];
            return;
        }else{
            // 修改组名
            NSDictionary *dic = @{
                                @"groupName":self.Textfiled.text,
                                @"groupType":@"1",
                                @"groupId":self.GroupIdTitle,
                                @"pubPriType":@"1"
                                };
            NSLog(@"+++%@",dic);
            [HttpsTool post:@"/web/group/recovery/update" params:dic success:^(id response) {
                NSLog(@"%@",response);
                NSLog(@"%@",response[@"msg"]);
                [BRProgressHUD showErrorWithStatus:response[@"msg"]];
                [BRProgressHUD dismissWithDelay:1.25];
                [self.navigationController popViewControllerAnimated:YES];
            } failure:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
    }
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];// 1
    // 进入页面直接为输入状态
    [self.Textfiled becomeFirstResponder];// 2
}
// 点击其他地方收回键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
@end
